package com.zyz.sunliky.controller;

import com.zyz.sunliky.common.ResultCommon;
import com.zyz.sunliky.entity.RequestData;
import com.zyz.sunliky.service.FinanceService;
import com.zyz.sunliky.service.SupplierService;
import jakarta.annotation.Resource;
import org.apache.poi.hpsf.Decimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@RequestMapping("/finance")
@RestController
public class FinanceController {

    public static final Logger logger = LoggerFactory.getLogger(FinanceController.class);

    @Autowired
    FinanceService financeService;

    @Resource
    SupplierService supplierService;
    /**
     * 供应商基础信息建立
     * @param id
     * @return
     */
    @GetMapping("/historical")
    public ResultCommon getSupplier(String id){
        try {

           Map map = financeService.getHistorical(id);

            return new ResultCommon(map,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    /**
     * 查询样品历史数据
     * @param requestData
     * @return
     */
    @PostMapping("/sample")
    public ResultCommon getQuantity(@RequestBody RequestData requestData){
        try {
            Map<String,Decimal> map = financeService.getHisQuantity(requestData);
            return new ResultCommon(map,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    /**
     * 领料单
     * @param id
     * @return
     */
    @GetMapping("/outbound")
    public ResultCommon getOrderNumber(String type,String id){
        try {
            List<Map<String,Object>> list = financeService.getOrderNumber(type,id);
            return new ResultCommon(list,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    /**
     * 销售价格从新提交
     * @param tokenid
     * @return
     */
    @GetMapping("/copy")
    public ResultCommon getPriceChange(String id,String tokenid){
        try {
            String count = financeService.getPriceChange(id,tokenid);
            return new ResultCommon(count,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    /**
     * 领料单
     * @param id
     * @return
     */
    @GetMapping("/status")
    public ResultCommon getSupplier(String id,String type){
        try {
            financeService.getStatus(id);
            return new ResultCommon("",200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    @GetMapping("/report")
    public ResultCommon getSupplier(){
        try {
            supplierService.saveRreport();
            return new ResultCommon("",200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }


    @GetMapping("/cost")
    public ResultCommon getCost(String id){
        try {

            BigDecimal cost = financeService.getCost(id);

            return new ResultCommon(cost,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    /**
     * 销售价格从新提交
     * @param tokenid
     * @return
     */
    @GetMapping("/payment")
    public ResultCommon getPayment(String id,String tokenid){
        try {
            String count = financeService.getPayment(id,tokenid);
            return new ResultCommon(count,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }

    }

    @GetMapping("/sales")
    public ResultCommon getSales(String id){
        try {
            List<Map<String, Object>> cost = financeService.getsales(id);
            return new ResultCommon(cost,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }
    }

    /**
     * 客户报价历史报价
     * @param xsxh
     * @return
     */
    @GetMapping("/quotation")
    public ResultCommon getPrice(String xsxh){
        try {
            Map<String, Object>  sales = financeService.getPrice(xsxh);
            return new ResultCommon(sales,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }
    }


    /**
     * 获取产品成本
     * @param id
     * @return
     */
    @GetMapping("/getProductCost")
    public ResultCommon getProductCost(String id){
        try {
            Map<String,BigDecimal> sales = financeService.getProductCost(id);
            return new ResultCommon(sales,200,"成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResultCommon(null,500,e.getMessage());
        }
    }

}
